
#include <BFile>
#include <BXmlDocument>
#include "Config.h"

Config::Config(const BString& name) : BObject(name)
{
	_dirty = false;
	_scrollStep = 0.0f;
	_language = "english";
	_headerPath = "../../include";
	_manualPath = "../../manual";
}
Config::~Config()
{

}

bool Config::load()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Read))
	{
		BFile::Create(fileName);
		return false;
	}

	BString errorMsg;
	BXmlDocument xmldoc;
	BXmlNode* root = xmldoc.read(file);
	if (!root)
	{
		return false;
	}
	if (BXmlNode* scrollStepNode = root->child("scroll-step"))
	{
		_scrollStep = scrollStepNode->text().toFloat();
	}
	if (BXmlNode* spreadSizeNode = root->child("spread-size"))
	{
		int spreadWidth = spreadSizeNode->attrib("width").toInt();
		int spreadHeight = spreadSizeNode->attrib("height").toInt();
		_spreadSize.set(spreadWidth, spreadHeight);
	}
	if (BXmlNode* languageNode = root->child("language"))
	{
		_language = languageNode->text();
	}
	if (BXmlNode* headerPathNode = root->child("header-path"))
	{
		_headerPath = headerPathNode->text();
	}
	if (BXmlNode* manualPathNode = root->child("manual-path"))
	{
		_manualPath = manualPathNode->text();
	}
	_dirty = false;
	return true;
}
bool Config::save()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Write))
	{
		return false;
	}

	BXmlDocument xmldoc;
	xmldoc.setFormat("/>\n");
	BXmlNode* root = new BXmlNode("config");
	root->setFormat(">\n</>\n");
	if (BXmlNode* scrollStepNode = root->addChild("scroll-step"))
	{
		scrollStepNode->setText(_scrollStep);
	}
	if (BXmlNode* spreadSizeNode = root->addChild("spread-size"))
	{
		spreadSizeNode->setAttrib("width", _spreadSize.width());
		spreadSizeNode->setAttrib("height", _spreadSize.height());
	}
	if (BXmlNode* languageNode = root->addChild("language"))
	{
		languageNode->setText(_language);
	}
	if (BXmlNode* headerPathNode = root->addChild("header-path"))
	{
		headerPathNode->setText(_headerPath);
	}
	if (BXmlNode* manualPathNode = root->addChild("manual-path"))
	{
		manualPathNode->setText(_manualPath);
	}
	bool res = xmldoc.write(file, root);
	int len = file.size();

	_dirty = false;
	return true;
}
bool Config::isDirty() const
{
	return _dirty;
}

void Config::setScrollStep(float scrollStep)
{
	if (_scrollStep != scrollStep)
	{
		_scrollStep = scrollStep;
		_dirty = true;
		this->emit(Signal_Changed);
	}
}
float Config::scrollStep() const
{
	return _scrollStep;
}

void Config::setSpreadSize(const BSize& spreadSize)
{
	if (_spreadSize != spreadSize)
	{
		_spreadSize = spreadSize;
		_dirty = true;
		this->emit(Signal_Changed);
	}
}
const BSize& Config::spreadSize() const
{
	return _spreadSize;
}

void Config::setLanguage(const BString& language)
{
	if (_language != language)
	{
		_language = language;
		_dirty = true;
		this->emit(Signal_Changed);
	}
}
const BString& Config::language() const
{
	return _language;
}

void Config::setHeaderPath(const BString& headerPath)
{
	if (_headerPath != headerPath)
	{
		_headerPath = headerPath;
		_dirty = true;
		this->emit(Signal_Changed);
	}
}
const BString& Config::headerPath() const
{
	return _headerPath;
}

void Config::setManualPath(const BString& manualPath)
{
	if (_manualPath != manualPath)
	{
		_manualPath = manualPath;
		_dirty = true;
		this->emit(Signal_Changed);
	}
}
const BString& Config::manualPath() const
{
	return _manualPath;
}


